/* eslint-disable @typescript-eslint/no-explicit-any */
import axios, { type AxiosRequestConfig } from 'axios'

interface Res<T> {
  success: boolean
  message: string
  data: T
}
const instance = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_URL,
  timeout: 30000,
})

instance.interceptors.request.use(config => {
  return config
})
instance.interceptors.response.use(
  res => {
    return res
  },
  err => {
    console.log(err)
  }
)
function request<T = any>(opts: AxiosRequestConfig) {
  return instance<any, Res<T>>(opts).then(res => res.data)
}

request.get = <T = any>(url: string, params?: any, config?: AxiosRequestConfig) =>
  request<T>({
    ...config,
    url,
    method: 'get',
    params,
  })
request.post = <T = any>(url: string, data?: any, config?: AxiosRequestConfig) =>
  request<T>({
    ...config,
    url,
    method: 'post',
    data,
  })
request.patch = <T = any>(url: string, data?: any, config?: AxiosRequestConfig) =>
  request<T>({
    ...config,
    url,
    method: 'patch',
    data,
  })

request.delete = <T = any>(url: string, data?: any, config?: AxiosRequestConfig) =>
  request<T>({
    ...config,
    url,
    method: 'delete',
    data,
  })

export default request
